package com.ruoyi.mateproduction.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateCleanRecord;
import com.ruoyi.mateproduction.service.MateCleanRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0 清场记录
 */
@RestController
@RequestMapping("/mateCleanRecord")
public class MateCleanRecordContorller extends BaseController {
    @Autowired
    MateCleanRecordService mateCleanRecordService;

    /**
     * 保存清场记录
     */
    @Log(title = "保存清场记录", businessType = BusinessType.INSERT)
    @PostMapping("/addMateCleanRecord")
    public AjaxResult addMateCleanRecord(@RequestBody MateCleanRecord mateCleanRecord) {
        mateCleanRecord.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mateCleanRecordService.addMateCleanRecord(mateCleanRecord);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询清场记录
     */
    @GetMapping("/cleanRecordList")
    @ResponseBody
    public TableDataInfo cleanRecordList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                         @RequestParam(value = "endTime", required = false) String endTime) {
        MateCleanRecord mateCleanRecord = new MateCleanRecord();
        mateCleanRecord.setBeginTime(beginTime);
        mateCleanRecord.setEndTime(endTime);
        startPage();
        mateCleanRecord.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateCleanRecord> list = mateCleanRecordService.cleanRecordList(mateCleanRecord);
        return getDataTable(list);
    }

    /*
       查询已经做完的清场记录
*/
    @GetMapping("/selectCleanRecord")
    public MateCleanRecord selectCleanRecord(@RequestParam(value = "cleanId", required = false) Long cleanId) {
        MateCleanRecord mateCleanRecord = mateCleanRecordService.selectCleanRecord(cleanId);
        return mateCleanRecord;
    }

    /**
     * 删除清场记录
     */
    @Log(title = "删除清场记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{cleanId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("cleanId") Long cleanId) {
        mateCleanRecordService.remove(cleanId);
        return AjaxResult.success("删除成功");
    }

    /*
        编辑清场记录
           */
    @PostMapping("/updateMateCleanRecord")
    @ResponseBody
    public AjaxResult updateMateCleanRecord(@RequestBody MateCleanRecord mateCleanRecord) {
        int num = mateCleanRecordService.updateMateCleanRecord(mateCleanRecord);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /*
   查询审核完的清场记录
     */
    @GetMapping("/auditCleanRecord")
    public Map<String, Object> auditCleanRecord() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateCleanRecord> mateCleanRecord = mateCleanRecordService.auditCleanRecord(SecurityUtils.getDeptId());
        resMap.put("mateCleanRecord", mateCleanRecord);
        return resMap;
    }

}
 